LINQ to XML - XName Samples


LINQ, LINQ to XML, .NET Framework 4.0
LINQ, LINQ to XML, XML, Console Window, XML Documentation
Desktop, Data
en-US
6/28/2011

Introduction

These samples demonstrate using XNamespace in LINQ to XML to work with XML Namespaces.

Building the Sample

  1. Open Program.cs
  2. Uncomment the line for the sample that you want to run; comment ones you don't want to run
  3. Press Ctrl + F5

Screenshot

Sample Code

C#
Edit|Remove
            [Category("XName")] 
            [Description("Find xsd:element with name=FullAddress")] 
            public void XLinq87() 
            { 
                XDocument doc = XDocument.Load("nw_customers.xsd"); 
                XNamespace XSD = "http://www.w3.org/2001/XMLSchema"; 
                XElement result = doc.Descendants(XSD + "element") 
                                     .Where(e => (string)e.Attribute("name") == "FullAddress") 
                                     .First(); 
                Console.WriteLine(result); 
            }

Source Code Files

More Information

For more information on LINQ to XML: http://msdn.microsoft.com/en-us/library/bb387098.aspx